package com.itheima.stringapi;

public class CaptchaGenerator {
    // 验证码的字符集
    private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    // 验证码的长度
    private static final int LENGTH = 4;

    public static void main(String[] args) {
        String captcha = generateCaptcha();
        System.out.println(captcha);
    }

    public static String generateCaptcha() {
        // 生成验证码
        StringBuilder captcha = new StringBuilder();
        // 循环生成验证码的每一位
        for (int i = 0; i < LENGTH; i++) {
            // 生成随机索引，获取验证码的字符
            int index = (int) (Math.random() * CHARS.length());
            // 将验证码的字符添加到StringBuilder中
            captcha.append(CHARS.charAt(index));
        }
        // 返回验证码
        return captcha.toString();
    }

}
